package com.vivo.ic.dm;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.vivo.ic.VLog;
import com.vivo.ic.dm.impl.DownloadNetChangeInterface;

public class DownloadNetChangeDealer implements DownloadNetChangeInterface {
    private static final String TAG = Constants.PRE_TAG + "DownloadNetChangeDealer";
    private boolean mNetChangeDoing = false;
    private int mDelayTime = 0;
    private final Context mContext;

    public DownloadNetChangeDealer(Context context) {
        mContext = context.getApplicationContext();
    }

    @Override
    public boolean handleNetChange() {
        VLog.d(TAG, "handleNetChange()");
        if (mNetChangeDoing) {
            mDelayTime = 1000;
            VLog.d(TAG, "net change is doing, delay 1000 ms");
        } else {
            mDelayTime = 0;
            VLog.d(TAG, "net change is not doing, no delay");
        }
        DownloadWorker.runOnWorkerThread(new Runnable() {
            @Override
            public void run() {
                mNetChangeDoing = true;
                NetworkInfo info = DMUtil.getActiveNetworkInfo();

                if (info == null) {
                    VLog.d(TAG, "abort handleNetChange because NetworkInfo is null");
                    mNetChangeDoing = false;
                    return;
                }

                if (DMUtil.isNeverRemind()) { // 不再提示
                    DownloadManager.startDownloadService(mContext, "net change Never Remind true " + info.getState());
                    NetDealHelper.startDownloadNetPaused(mContext.getContentResolver());
                    VLog.d(TAG, "just start service in handleNetChange because of user choose never remind ");
                    mNetChangeDoing = false;
                    return;
                }

                VLog.d(TAG, "handleNetChange info state is " + info.getState() + " info type is " + info.getType());
                DownloadManager manager = DownloadManager.getInstance();
                if (info.isConnected()) {
                    switch (info.getType()) {
                    case ConnectivityManager.TYPE_WIFI:
                        manager.setLastNetType(Constants.CHANGE_STATE_WIFI);

                        // 取消移动网络切换的notification
                        manager.getNotifier().hideNetPauseNotification();

                        // 移动网络切回wifi环境，继续下载因为网络切换而暂停的下载项
                        NetDealHelper.startDownloadNetPaused(mContext.getContentResolver());
                        DownloadManager.startDownloadService(mContext, "net change TYPE_WIFI now");

                        break;
                    case ConnectivityManager.TYPE_MOBILE:
                        manager.setLastNetType(Constants.CHANGE_STATE_MOBILE);

                        // 切换到移动网络时，根据情况来决定是否暂停下载
                        NetDealHelper.netPauseDownload(mContext.getContentResolver());
                        break;
                    default:
                        break;
                    }
                }
                mNetChangeDoing = false;
            }
        }, mDelayTime, Thread.NORM_PRIORITY);
        return true;
    }

}
